package concurrencyCookBook;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Date;

/**
 * 
 *Thread
 *id:保存线程的唯一id属性
 *name:保存线程的名字
 *priority:线程的优先级1到10
 *status:线程的状态[new,runnable,blocked,waiting,time waiting,terminated]
 */
/**
 * 当所有的非守护线程结束或者其中有一个线程调用了System.exit()方法这个应用程序才可能结束
 *
 */
/**
 * ThreadGroup tg=new ThreadGroup("threadGroup");
 *单个线程会覆盖线程组的处理异常方法
 */
/**
 *  NEW: Thread state for a thread which has not yet started.
 *  
 *  RUNNABLE:Thread state for a runnable thread.  A thread in the runnable
 *  	state is executing in the Java virtual machine but it may
 *  	be waiting for other resources from the operating system
 *  	such as processor
 *  
 *  BLOCKED:
 *  	Thread state for a thread blocked waiting for a monitor lock.
 *  	A thread in the blocked state is waiting for a monitor lock
 *  	to enter a synchronized block/method or
 *  	reenter a synchronized block/method after calling
 *  	{@link Object#wait() Object.wait}. 
 *  
 *  WAITING:Thread state for a waiting thread with a specified waiting time.
 *       A thread is in the timed waiting state due to calling one of
 *       the following methods with a specified positive waiting time
 *  
 *  TIMED_WAITING:Thread state for a waiting thread with a specified waiting time.
 *  	A thread is in the timed waiting state due to calling one of
 *  	the following methods with a specified positive waiting time:
 *  
 *  TERMINATED:Thread state for a terminated thread.The thread has completed execution.
 *
 */
public class One {

	public static void main(String[] args) throws InterruptedException {
		A a=new A();
		ThreadGroup tg=new ThreadGroupUncaught("线程组");
		//创建分组线程
		Thread t=new Thread(tg,a);
		System.out.println(t.getState()+"getState线程的状态");
		t.setUncaughtExceptionHandler(new ThreadUncaughtException());
		t.start();
		t.join();
		//中断线程
		t.interrupt();
		System.out.println(t.isInterrupted()+" isInterrupted是否被终断");
		Thread.interrupted();
		//System.exit(0);
		Thread.sleep(1000);
		System.out.println(t.getState()+"线程的状态");
	}

}
class ThreadGroupUncaught extends ThreadGroup{
	public ThreadGroupUncaught(String name) {
		super(name);
	}

	public void uncaughtException(Thread t, Throwable e) {
		System.out.println("此线程组报错调用方法");
		super.uncaughtException(t, e);
	}
}
class ThreadUncaughtException implements UncaughtExceptionHandler{

	@Override
	public void uncaughtException(Thread t, Throwable e) {
		System.out.println("如果线程报错调用此方法");
	}
	
}
class A implements Runnable{
	//线程局部变量,这样就没有线程共享问题了
	private static ThreadLocal<Date> localDate=new ThreadLocal<Date>(){

		@Override
		protected Date initialValue() {
			// TODO Auto-generated method stub
			return new Date();
		}
		
	};
	@Override
	public void run() {
		//System.out.println(1/0+"线程1/0报错");
		System.out.println(localDate.get());
		System.out.println(Thread.currentThread().getState());
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getState());
	}
	
}
